/**
 * Copyright 2009 by Christian Scheid. This file is part of the switch eclipse plugin and is licensed under the
 * GPL version 3. Please refer to the URL http://www.gnu.org/licenses/gpl-3.0.html for details.
 */
package org.digitalsoul.switchplugin.prefs;

import org.digitalsoul.switchplugin.prefs.util.ItemListConfigurator;
import org.digitalsoul.switchplugin.prefs.util.UITools;
import org.eclipse.jface.wizard.IWizard;
import org.eclipse.swt.SWT;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Event;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.List;
import org.eclipse.swt.widgets.Listener;


public abstract class CriteriaListUI {

    protected ItemListConfigurator configurator;
    protected List list;
    private Button addButton;
    private Button removeButton;

    public CriteriaListUI(final ItemListConfigurator configurator) {
        this.configurator = configurator;
    }
    
    public void build(Composite mainPanel) {
        buildLabels(mainPanel);
        buildListView(mainPanel);
        updateListItems();
        updateButtons();
    }
    
    private void buildLabels(Composite mainPanel) {
        Label label = new Label(mainPanel, SWT.NONE);
        label.setText(getHeader());
        label.setLayoutData(UITools.getTwoSpanGridData());
        Label helpLabel = UITools.addHelpLabel(mainPanel, getHelpText());
        helpLabel.setLayoutData(UITools.getTwoSpanGridData());
    }
    
    protected void buildListView(final Composite mainPanel) {
        list = new List(mainPanel, SWT.BORDER | SWT.SINGLE | SWT.V_SCROLL);
        GridData data = new GridData(GridData.FILL_BOTH);
        data.minimumHeight = 80;
        data.grabExcessVerticalSpace = true;
        data.verticalAlignment = GridData.BEGINNING;
        list.setLayoutData(data);
        list.addListener(SWT.Selection, new Listener() {
            public void handleEvent(Event arg0) {
               updateButtons();
            }
        });

        // BUTTONS
        Composite buttonPanel = new Composite(mainPanel, SWT.NONE);
        buttonPanel.setLayout(new GridLayout());
        data = new GridData();
        data.verticalAlignment = GridData.BEGINNING;
        buttonPanel.setLayoutData(data);
        
        addButton = UITools.addButton(buttonPanel, "New...");    
        addButton.addListener(SWT.Selection, new Listener() {

            // NEW
            public void handleEvent(Event event) {
               UITools.openWizard(getWizard());
            }
        });
        
        removeButton = UITools.addButton(buttonPanel, "Remove");    
        removeButton.addListener(SWT.Selection, new Listener() {

            // REMOVE
            public void handleEvent(Event event) {
                int index = list.getSelectionIndex();
                removeItem(index);
                updateListItems();
                UITools.selectListAfterRemove(list, index);
                updateButtons();
            }
        });
    }
    
    protected abstract void removeItem(int selectionIndex);

    protected abstract IWizard getWizard();

    public void updateListItems() {
        list.removeAll();
        if (hasListItems()) {
            String[] listItems = getListItems();
            list.setItems(listItems);
        }
        list.setEnabled(hasListItems());
        list.update();
    }

    private boolean hasListItems() {
        return getListItems() != null && getListItems().length > 0;
    }
    
    public void updateButtons() {
        removeButton.setEnabled(list.getSelectionIndex() >= 0);
        addButton.setEnabled(configurator.hasItemSelection());
    }
    
    public void itemSelectionChanged() {
        updateListItems();
        updateButtons();
    }

    protected abstract String[] getListItems();
    protected abstract String getHeader();
    protected abstract String getHelpText();
}
